/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "PlayerUnit.h"

#include "CameraObject.h"//in engine

#include "GameScene.h"

namespace PQClient
{
	PlayerUnit::PlayerUnit(Ogre::String name,PQEngine::SceneObject* scene, Unit::Type type)
		: Unit(name,scene,type),_leftWeapon(0),_rightWeapon(0),_show(false)
	{

		setupBody();
		setupEquipment();
		selectAnim(Player_Anim_Idle);

		this->setVisible(false);
	}

	void PlayerUnit::setupBody()
	{

		//we now hard code,will use config file
		switch(_type){
			case Unit::Unit_Player_Destroyer:
				loadDestroyer();
				break;
			case Unit::Unit_Player_Alchemist:
				loadAlchemist();
				break;
			case Unit::Unit_Player_Vanquisher:
				loadVanquisher();
				break;
		}


	}

	void PlayerUnit::setupEquipment()
	{

		// create swords and attach to sheath
		_leftWeapon=_sceneObject->createEntity("2handers_01.MESH");
		_meshObject->getEntity()->attachObjectToBone("tag_lefthand",_leftWeapon);

		_rightWeapon=_sceneObject->createEntity("2handers_01.MESH");
		_meshObject->getEntity()->attachObjectToBone("tag_righthand",_rightWeapon);


	}

	void PlayerUnit::setVisible(bool show)
	{
		_show=show;
		_meshObject->getMeshComponent()->setVisible(show);
		_leftWeapon->setVisible(show);
		_rightWeapon->setVisible(show);
	}

	void PlayerUnit::update(float deltaT)
	{
	}

	MeshObject* PlayerUnit::getMeshObject()
	{
		return _meshObject;
	}

	void PlayerUnit::changeRightWeapon()
	{
		_meshObject->getEntity()->detachObjectFromBone(_rightWeapon);

	}

	void PlayerUnit::changeLeftWeapon()
	{
		_meshObject->getEntity()->detachObjectFromBone(_leftWeapon);

	}

	void PlayerUnit::loadDestroyer()
	{
		_meshObject= _sceneObject->loadMeshObject("player.MESH");

		Ogre::String skelName="idle.SKELETON";
		_meshObject->addSkeletonAnim(skelName);



		_meshObject->getMeshComponent()->setAnim("idle");
		//_bodyMeshObj->getMeshComponent()->setAnimState(true);//run anim
	}

	void PlayerUnit::loadAlchemist()
	{
		_meshObject= _sceneObject->loadMeshObject("Alchemist.MESH");

		Ogre::String skelName="Idle.SKELETON";
		_meshObject->addSkeletonAnim(skelName);

		_meshObject->getMeshComponent()->setAnim("Idle");
		//_bodyMeshObj->getMeshComponent()->setAnimState(true);//run anim
	}

	void PlayerUnit::loadVanquisher()
	{
		_meshObject= _sceneObject->loadMeshObject("Vanquisher.MESH");

		Ogre::String skelName="Idle.SKELETON";
		_meshObject->addSkeletonAnim(skelName);

		_meshObject->getMeshComponent()->setAnim("Idle");
	}

	void PlayerUnit::setPosition( float x,float y,float z )
	{
		//if(_meshObject){
			_meshObject->setPosition(x,y,z);
		//}
	}

	Ogre::Vector3 PlayerUnit::getPosition()
	{
		//if(_meshObject){
			return _meshObject->getPosition();
		//}
		//return Ogre::Vector3();
	}

	void PlayerUnit::setScale( float x,float y,float z )
	{
		//if(_meshObject){
			_meshObject->setScale(Ogre::Vector3(x,y,z));
		//}
	}

	const Ogre::Quaternion &PlayerUnit::getOrientation()
	{
		//if(_meshObject){
			return _meshObject->getOrientation();
		//}
	}

	void PlayerUnit::selectAnim(AnimType type)
	{
		// this is very important due to the nature of the exported animations
		_meshObject->setBlendMode(Ogre::ANIMBLEND_CUMULATIVE);
		_meshObject->getMeshComponent()->enableAnim(true);

		/*
		

		Ogre::String animNames[] =
		{"IdleBase", "IdleTop", "RunBase", "RunTop", "HandsClosed", "HandsRelaxed", "DrawSwords",
		"SliceVertical", "SliceHorizontal", "Dance", "JumpStart", "JumpLoop", "JumpEnd"};

		// populate our animation list
		for (int i = 0; i < NUM_ANIMS; i++){
			anims[i] = bodyEntity->getAnimationState(animNames[i]);
			anims[i]->setLoop(true);
			fadingIn[i] = false;
			fadingOut[i] = false;
		}

		// start off in the idle state (top and bottom together)
		setBaseAnimation(ANIM_IDLE_BASE);
		setTopAnimation(ANIM_IDLE_TOP);

		// relax the hands since we're not holding anything
		anims[ANIM_HANDS_RELAXED]->setEnabled(true);

		swordsDrawn = false;
		*/
	}

}
